package com.aat.dxfy.concurrent.demo1;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureTest {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService exec = Executors.newCachedThreadPool();
		Runnable task1 = new Runnable() {
			int i = 0;

			public void run() {
				boolean b = true;
				while (b) {
					System.out.println("test1 " + i++);
					if (i == 10) {
						b = false;
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println("t1 ok");
			}
		};

		Callable<Integer> task2 = new Callable<Integer>() {

			public Integer call() throws Exception {
				int i = 0;
				boolean b=true;
				while (b) {
					System.out.println("test2 " + i++);
					if (i == 10) {
						b=false;
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println("t2 ok");
				return i;
			}
		};

		Future<?> f1 = exec.submit(task1);
		Future<Integer> f2 = exec.submit(task2);
		while (f1.isDone() && f2.isDone()) {
			System.out.println(f2.get());
			System.exit(0);
		}
	}

}
